package com.wfy.modules.sys.web;

import com.google.common.collect.Maps;
import com.wfy.common.entity.Result;
import com.wfy.common.entity.ResultGenerate;
import com.wfy.common.security.shiro.utils.ShiroHolder;
import com.wfy.common.web.BaseController;
import com.wfy.modules.sys.dto.LoginDto;
import com.wfy.modules.sys.secutiry.UsernamePasswordToken;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2017/10/14
 * Time: 09:24
 */
@Controller
@Api(value = "系统功能接口")
@RequestMapping(value = "/v1/")
public class SystemController extends BaseController {

    // ================================================================
    // Constants
    // ================================================================

    // ================================================================
    // Fields
    // ================================================================


    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    // ================================================================
    // Public or Protected Methods
    // ================================================================

    @GetMapping(value = "login")
    public String login() {
        if (ShiroHolder.isAuthenticated() || ShiroHolder.getUser() != null) {
            return REDIRECT + "/";
        } else {
            return "/login.html";
        }
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Result loginFail(@Valid @RequestBody LoginDto userDto) {
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken();
        usernamePasswordToken.setUsername(userDto.getUsername());
        usernamePasswordToken.setPassword(userDto.getPassword().toCharArray());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
        } catch (AuthenticationException e) {
            return ResultGenerate.getFailResult("用户名或密码错误");
        }

        Map<String, Object> result = Maps.newHashMap();
        result.put("token", subject.getSession().getId());
        return ResultGenerate.getSuccessResult(result);
    }


    @GetMapping(value = "logout")
    @ResponseBody
    public Result logout() {
        ShiroHolder.getSubject().logout();
        return ResultGenerate.getSuccessResult();
    }

    // ================================================================
    // Getter & Setter
    // ================================================================

    // ================================================================
    // Private Methods
    // ================================================================


    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
